import DateUtil from "../../../engine/DateUtil";
import { EngineConfig } from "../../../engine/EngineConfig";
import { Logger } from "../../../engine/Logger";
import ViewBase from "../../../engine/ViewBase";
import { HttpHelper } from "../../../net/HttpHelper";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubStatics extends ViewBase {

    private txtDate: cc.Label;
    private txtJu: cc.Label
    private startDate: Date
    private ju: number = 0

    public onInitChild(...optionalParams: any[]): void {
        this.txtDate = this.getChild(this.node, "txtDate", cc.Label)
        this.txtJu = this.getChild(this.node, "txtJu", cc.Label)
        let today: Date = new Date()
        this.startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate())
        let btnNextDate: cc.Node = this.findChild(this.node, "btnNextDate")
        let btnPreDate: cc.Node = this.findChild(this.node, "btnPreDate")
        UIHelper.onAddEvent(btnNextDate, () => {
            this.startDate = new Date(this.startDate.getTime() + EngineConfig.OneDay)
            this.refresh()
            this.getDateInfo()
        })
        UIHelper.onAddEvent(btnPreDate, () => {
            this.startDate = new Date(this.startDate.getTime() - EngineConfig.OneDay)
            this.refresh()
            this.getDateInfo()
        })
        this.refresh()
        this.getDateInfo()
    }

    private getDateInfo() {
        HttpHelper.get_club_room_statics(UserModel.instance.nowClub.clubid, this.startDate.getTime(), (data:number) => {
            this.ju = data
            this.refresh()
        })
    }

    private refresh() {
        this.txtDate.string = DateUtil.formatDateStr3(this.startDate.getTime())
        this.txtJu.string = "" + this.ju
    }
}
